package mysql

import (
	"cloud-storage/global"
	"strconv"
	"time"
)

// FileFolder 文件夹表
type FileFolder struct {
	Id             int
	FileFolderName string //文件夹名
	ParentFolderId int    //父文件夹id
	FileStoreId    int    //文件仓库id
	Time           string
}

// CreateFolder 新建文件夹
func CreateFolder(folderName, parentId string, fileStoreId int) error {
	parentIdInt, err := strconv.Atoi(parentId)
	if err != nil {
		return err
	}
	fileFolder := FileFolder{
		FileFolderName: folderName,
		ParentFolderId: parentIdInt,
		FileStoreId:    fileStoreId,
		Time:           time.Now().Format("2006-01-02 15:04:05"),
	}
	return global.DB.Create(&fileFolder).Error
}

// GetFileFolderCount 获取用户文件夹数量
func GetFileFolderCount(fileStoreId int) (count int64, err error) {
	err = global.DB.Model(&FileFolder{}).Where("file_store_id = ?", fileStoreId).Count(&count).Error
	return
}

// GetCurrentFolder 获取当前文件夹信息
func GetCurrentFolder(fId string) (fileFolder FileFolder, err error) {
	err = global.DB.Find(&fileFolder, "id = ?", fId).Error
	return
}

// UpdateFolderName 更改文件夹名
func UpdateFolderName(fId, fName string) error {
	var fileFolder FileFolder
	err := global.DB.Model(&fileFolder).Where("id = ?", fId).Update("file_folder_name", fName).Error
	return err
}

// GetFileFolder 获取当前目录的所有文件夹
func GetFileFolder(parentId string, fileStoreId int) (fileFolder []FileFolder, err error) {
	err = global.DB.Where("parent_folder_id = ? and file_store_id = ?", parentId, fileStoreId).Find(&fileFolder).Error
	return
}

// GetParentFolder 获取父类的id
func GetParentFolder(fId string) (fileFolder FileFolder, err error) {
	err = global.DB.Find(&fileFolder, "id = ?", fId).Error
	return
}

// GetCurrentAllParent 获取当前路径所有的父级
func GetCurrentAllParent(folder FileFolder, folders []FileFolder) []FileFolder {
	var parentFolder FileFolder
	if folder.ParentFolderId != 0 {
		global.DB.Find(&parentFolder, "id = ?", folder.ParentFolderId)
		folders = append(folders, parentFolder)
		//递归查找当前所有父级
		return GetCurrentAllParent(parentFolder, folders)
	}

	//反转切片
	for i, j := 0, len(folders)-1; i < j; i, j = i+1, j-1 {
		folders[i], folders[j] = folders[j], folders[i]
	}

	return folders
}

// FolderIsExist 判断该目录下是否已经有该文件夹
func FolderIsExist(folderName string, parentId string) bool {
	var count int64
	global.DB.Model(FileFolder{}).Where("file_folder_name = ? AND parent_folder_id = ?", folderName, parentId).Count(&count)
	if count > 0 {
		return false
	}
	return true
}

// DeleteFileFolder 删除文件夹信息
func DeleteFileFolder(fId string) bool {
	var fileFolder FileFolder
	var fileFolder2 FileFolder
	//删除文件夹信息
	global.DB.Where("id = ?", fId).Delete(FileFolder{})
	//删除文件夹中文件信息
	global.DB.Where("parent_folder_id = ?", fId).Delete(MyFile{})
	//删除文件夹中文件夹信息
	global.DB.Find(&fileFolder, "parent_folder_id = ?", fId)
	global.DB.Where("parent_folder_id = ?", fId).Delete(FileFolder{})

	global.DB.Find(&fileFolder2, "parent_folder_id = ?", fileFolder.Id)
	if fileFolder2.Id != 0 { //递归删除文件下的文件夹
		return DeleteFileFolder(strconv.Itoa(fileFolder.Id))
	}

	return true
}
